<template>
  <div class="mod-prod-swiper">
    <el-tabs class="title" v-model="activeTabIndex" @tab-click="tabChange">
      <el-tab-pane name="1" :label="$t('publics.swiperConfig')"></el-tab-pane>
    </el-tabs>
    <div class="search-bar">
      <el-form
        @submit.native.prevent
        :inline="true"
        class="search-form"
        ref="searchForm"
        :model="searchForm"
        size="small"
      >
        <div class="input-row">
          <el-form-item
            prop="status"
            :label="$t('publics.status') + ':'"
            class="search-form-item"
          >
            <el-select
              v-model="searchForm.status"
              :placeholder="$t('publics.status')"
            >
              <el-option :label="$t('publics.disable')" :value="0"></el-option>
              <el-option :label="$t('publics.normal')" :value="1"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item
            :label="$t('weixin.createtime') + ':'"
            label-width="70px"
          >
            <el-date-picker
              v-model="dateRange"
              @change="changeDateRange"
              type="datetimerange"
              :range-separator="$t('coupon.to')"
              value-format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="$t('coupon.startTime')"
              :end-placeholder="$t('coupon.endTime')"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>

          <el-form-item style="float: right">
            <div class="default-btn primary-btn" @click="searchChange(true)">
              {{ $t("product.search") }}
            </div>
            <div class="default-btn" @click="resetSearchForm('searchForm')">
              {{ $t("product.reset") }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <!-- <el-checkbox
          v-model="selectAll"
          @change="handleSelectAll"
          class="all-check-btn"
          v-if="dataList.length"
          >{{ $t("publics.selectAll") }}</el-checkbox
        > -->
        <div
          class="default-btn primary-btn"
          v-if="isAuth('platform:indexImg:save')"
          @click.stop="addOrUpdateHandle()"
        >
          {{ $t("crud.addTitle") }}
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('publics.maxIndexImgNumOfPlatform')"
            placement="top"
          >
            <i class="el-icon-warning"></i>
          </el-tooltip>
        </div>
        <div
          :class="[
            dataListSelections.length <= 0 ? 'disabled-btn' : '',
            'default-btn',
          ]"
          @click="deleteHandle()"
          v-if="isAuth('platform:indexImg:delete')"
        >
          {{ $t("sys.batchDelete") }}
        </div>
      </div>
      <div class="table-con banner-table">
        <template>
          <el-table
            ref="bannerListTable"
            :data="dataList"
            header-cell-class-name="table-header"
            row-class-name="table-row"
            @selection-change="handleSelectionChange"
            style="width: 100%"
          >
            <el-table-column
              type="index"
              :label="$t('formData.serialNumber')"
              prop="index"
              align="center"
            />
            <el-table-column type="selection" v-if="dataList.length" />
            <el-table-column
              prop="imgUrl"
              :label="$t('platform.carouselPicture')"
              width="440"
            >
              <template slot-scope="scope">
                <img
                  :src="scope.row.imgUrl"
                  height="100"
                  @error="handlePicError"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="seq"
              :label="$t('product.seq')"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="status"
              :label="$t('product.status')"
            >
              <template slot-scope="scope">
                <span class="tag-text">{{
                  [$t("brand.disable"), $t("brand.normal")][scope.row.status]
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="auditingTime"
              :label="$t('weixin.createtime')"
              sortable
            >
              <template slot-scope="scope">
                {{ scope.row.createTime }}
              </template>
            </el-table-column>
            <el-table-column
              fixed="right"
              align="center"
              :label="$t('publics.operating')"
            >
              <template slot-scope="scope">
                <div class="text-btn-con">
                  <div
                    class="default-btn text-btn"
                    v-if="isAuth('platform:indexImg:update')"
                    @click="addOrUpdateHandle(scope.row.imgId)"
                  >
                    {{ $t("user.editBtn") }}
                  </div>
                  <div
                    class="default-btn text-btn"
                    v-if="isAuth('platform:indexImg:delete')"
                    @click="deleteHandle(scope.row.imgId)"
                  >
                    {{ $t("user.deleteBtn") }}
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </div>
      <el-pagination
        v-if="dataList.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="refreshChange"
    ></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from "./indexImg-add-or-update";
  export default {
    data() {
      return {
        currentHandleItem: {},
        dateRange: [],
        theData: {}, // 保存上次点击查询的请求条件
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
        // 修改
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10, // 每页显示多少条
        },
        searchForm: {
          status: "",
        },
        selectAll: false,
        activeTabIndex: "1",
      };
    },
    components: {
      AddOrUpdate,
    },
    created() {
      this.getDataList();
    },
    methods: {
      changeDateRange(e) {
        if (e) {
          this.searchForm.startTime = e[0];
          this.searchForm.endTime = e[1];
        } else {
          delete this.searchForm.startTime;
          delete this.searchForm.endTime;
        }
      },
      tabChange(e) {
        this.dataList = [];
        console.log(this.activeTabIndex);
        if (this.activeTabIndex == 1) {
          delete this.searchForm.processStatus;
        } else {
          this.searchForm.processStatus = 0;
        }
        this.getDataList(1, true);
      },
      handlePicError(event) {
        event.target.src = require("@/assets/img/def.png");
      },
      /**
       * 获取图片路径
       */
      getImgSrc(img) {
        if (!img) {
          return "";
        }
        if (img.indexOf("http://") === 0 || img.indexOf("https://") === 0) {
          return img;
        }
        return this.resourcesUrl + img;
      },
      // 获取数据列表
      getDataList(page, newData = false) {
        if (this.page) {
          let size = Math.ceil(this.page.total / this.page.pageSize);
          this.page.currentPage =
            (this.page.currentPage > size ? size : this.page.currentPage) || 1;
        }
        this.dataListLoading = true;
        if (newData || !this.theData) {
          this.theData = JSON.parse(JSON.stringify(this.searchForm));
        }

        this.$http({
          url: this.$http.adornUrl("/platform/indexImg/page"),
          method: "get",
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize,
                imgType: 2,
              },
              this.theData
            )
          ),
        }).then(({ data }) => {
          this.dataList = data.records.map((m) => {
            m.imgUrl = this.getImgSrc(m.imgUrl);
            return m;
          });
          this.page.total = data.total;
          this.dataListLoading = false;
        });
      },

      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,{imgType:2});
        });
      },
      // 删除
      deleteHandle(id) {
        let ids = id
          ? [id]
          : this.dataListSelections.map((item) => {
              return item.imgId;
            });
        if (!id && !this.dataListSelections.length) {
          return;
        }
        let tip =
          this.$i18n.t("remindPop.makeSure") +
          "[" +
          (id
            ? this.$i18n.t("remindPop.delete")
            : this.$i18n.t("remindPop.batchDeletion"));
        tip = tip + "]" + this.$i18n.t("remindPop.operation") + "?";
        this.$confirm(tip, this.$i18n.t("remindPop.remind"), {
          confirmButtonText: this.$i18n.t("remindPop.confirm"),
          cancelButtonText: this.$i18n.t("remindPop.cancel"),
          type: "warning",
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl("/platform/indexImg"),
            method: "delete",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            this.page.total = this.page.total - ids.length;
            this.$message({
              message: this.$i18n.t("publics.operation"),
              type: "success",
              duration: 1500,
              onClose: () => {
                this.handleSelectAll();
                this.refreshChange();
              },
            });
          });
        });
      },
      // 条件查询
      searchChange(newData = false) {
        this.page.currentPage = 1;
        this.getDataList(this.page, newData);
      },
      // 刷新回调
      refreshChange() {
        this.getDataList(this.page);
      },
      // 多选变化
      handleSelectionChange(val) {
        this.dataListSelections = val;
        this.selectAll = val.length === this.dataList.length;
      },
      /**
       * 全选按钮
       */
      handleSelectAll() {
        if (this.$refs.bannerListTable.selection.length < this.dataList.length) {
          this.selectAll = true;
        } else {
          this.selectAll = false;
        }
        this.$refs.bannerListTable.toggleAllSelection();
      },

      /**
       * 重置表单
       * @param {String} formName 表单名称
       */
      resetSearchForm(formName) {
        this.$refs[formName].resetFields();
        this.dateRange = [];
        this.searchForm = {
          processStatus: this.searchForm.processStatus,
        };
        this.prodName = "";
        this.isTop = "";
        this.categoryId = null;
      },

      handleSizeChange(val) {
        this.page.pageSize = val;
        this.getDataList();
      },
      handleCurrentChange(val) {
        this.page.currentPage = val;
        this.getDataList();
      },
    },
  };
</script>

<style scoped>
  >>> .el-table__fixed-right {
    height: 98% !important;
  }
  >>> .el-table__fixed-right::before {
    background-color: transparent;
  }
  >>> .el-table::before {
    z-index: 6;
  }
</style>
